package com.liuyfly.leetcode.questions;

import com.liuyfly.leetcode.model.ListNode;
import com.liuyfly.leetcode.util.LeetcodeUtil;

/**
 * @author liuyangfang at 2022-09-05 15:33
 */
public class Question0023 {
    /**
     * 给你一个链表数组，每个链表都已经按升序排列。 请你将所有链表合并到一个升序链表中，返回合并后的链表。
     *
     * <pre>
     示例 1：
     输入：lists = [[1,4,5],[1,3,4],[2,6]]
     输出：[1,1,2,3,4,4,5,6]
     解释：链表数组如下：
     [
     1->4->5,
     1->3->4,
     2->6
     ]
     将它们合并到一个有序链表中得到。
     1->1->2->3->4->4->5->6
     * </pre>
     *
     * <pre>
     示例 2：
     输入：lists = []
     输出：[]
     * </pre>
     *
     * <pre>
     示例 3：
     输入：lists = [[]]
     输出：[]
     * </pre>
     *
     * </br>
     * </br>
     * 链接：https://leetcode.cn/problems/merge-k-sorted-lists
     */
    public static ListNode mergeKLists(ListNode[] lists) {
        if (lists.length <= 0) {
            return null;
        }
        for (int i = 1; i < lists.length; i++) {
            ListNode first = mergeKLists(lists, 0, i);
            lists[0] = first;
        }
        return lists[0];
    }

    public static ListNode mergeKLists(ListNode[] lists, int i, int j) {
        return mergeTwoLists(lists[i], lists[j]);
    }

    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        } else if (list2 == null) {
            return list1;
        }
        ListNode head = new ListNode();
        ListNode newCurrent = head;
        ListNode current1 = list1;
        ListNode current2 = list2;
        if (current1.val < current2.val) {
            head.val = current1.val;
            current1 = current1.next;
        } else {
            head.val = current2.val;
            current2 = current2.next;
        }

        while (current1 != null || current2 != null) {
            ListNode newListNode = new ListNode();
            if (current1 == null) {
                newListNode.val = current2.val;
                current2 = current2.next;
            } else if (current2 == null) {
                newListNode.val = current1.val;
                current1 = current1.next;
            } else if (current1.val < current2.val) {
                newListNode.val = current1.val;
                current1 = current1.next;
            } else {
                newListNode.val = current2.val;
                current2 = current2.next;
            }
            newCurrent.next = newListNode;
            newCurrent = newListNode;
        }
        return head;
    }

    public static void main(String[] args) {

        ListNode node1 = LeetcodeUtil.initSortedListNode(3);
        LeetcodeUtil.printListNode(node1);
        ListNode node2 = LeetcodeUtil.initSortedListNode(4);
        LeetcodeUtil.printListNode(node2);
        ListNode node3 = LeetcodeUtil.initSortedListNode(3);
        LeetcodeUtil.printListNode(node3);

        ListNode[] array = new ListNode[3];
        array[0] = node1;
        array[1] = node2;
        array[2] = node3;

        ListNode result = mergeKLists(array);
        LeetcodeUtil.printListNode(result);
    }
}
